package com.atguigu.jdbc2;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBPool {

    /*
        通过代码配置
     */
    @Test
    public void test() throws SQLException {
        //创建DruidDataSource对象
        DruidDataSource source = new DruidDataSource();
        //参数设置
        source.setUsername("root");
        source.setPassword("123321");
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:3306/myemployees");

        Connection connection = source.getConnection();
        System.out.println(connection);
    }

    /*
        通过配置文件配置
     */
    @Test
    public void test2() throws Exception {
        //读取配置文件中的内容
        Properties properties = new Properties();
        properties.load(new FileInputStream("druid.properties"));
        //创建DataSource对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //获取连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
}
